package com.lagou.user.controller;

import com.lagou.common.pojo.User;
import com.lagou.user.feign.AuthCodeFeignClient;
import com.lagou.user.service.ITokenService;
import com.lagou.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * created by kirito at 2020/7/5
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITokenService tokenService;
    @Autowired
    private AuthCodeFeignClient authCodeFeignClient;

    /**
     * 注册
     *
     * @param email           邮箱
     * @param password        密码
     * @param confirmPassword 确认密码
     * @param code            验证码
     * @return true/false
     */
    @PostMapping("/register/{email}/{password}/{confirmPassword}/{code}")
    public boolean register(@PathVariable(value = "email") String email,
                            @PathVariable(value = "password") String password,
                            @PathVariable(value = "confirmPassword") String confirmPassword,
                            @PathVariable(value = "code") String code) {
        // 重复注册校验
        if (isRegistered(email)) {
            return false;
        }
        // 验证码校验
        int validate = authCodeFeignClient.validateCode(email, code);
        if (0 != validate) {
            return false;
        }

        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setConfirmPassword(confirmPassword);
        userService.save(user);
        return true;
    }

    /**
     * 登录
     *
     * @param email    邮箱
     * @param password 密码
     * @return 邮箱
     */
    @PostMapping("/login/{email}/{password}")
    public boolean login(@PathVariable(value = "email") String email,
                         @PathVariable(value = "password") String password,
                         HttpServletResponse response) {
        User user = userService.findByEmailAndPassword(email, password);
        if (user == null) return false;
        // 存在，可以登录，写入token并返回
        String token = tokenService.createToken();
        tokenService.saveToken(email, token);
        Cookie cookie = new Cookie("lagou-cookie", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        return true;
    }

    /**
     * 是否已注册
     *
     * @param email 邮箱
     * @return true/false
     */
    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable(value = "email") String email) {
        return userService.isRegistered(email);
    }

    /**
     * 校验token是否合法
     *
     * @param token token
     * @return true/false
     */
    @GetMapping("/token/validate/{token}")
    public boolean validateToken(@PathVariable(value = "token") String token) {
        return userService.validateToken(token);
    }

    /**
     * 根据token查询⽤用户登录邮箱
     *
     * @param token token
     * @return 邮箱
     */
    @GetMapping("/info/{token}")
    public String getEmail(@PathVariable(value = "token") String token) {
        return userService.findEmailByToken(token);
    }

}
